package com.zhimeng.personal.utils;

import java.util.Calendar;

/**
 * Created by sunzhimeng on 10/31/17.
 */

public class DateUtils {
    public static String getCurrentDateFormat() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR) + "年"
                + (calendar.get(Calendar.MONTH) + 1) + "月"//从0计算
                + calendar.get(Calendar.DAY_OF_MONTH) + "日" + (calendar.get(Calendar.HOUR_OF_DAY) > 12 ? "下午" : "上午")
                + (calendar.get(Calendar.HOUR_OF_DAY) > 12 ? calendar.get(Calendar.HOUR_OF_DAY) - 12 : calendar.get(Calendar.HOUR_OF_DAY)) + ":"
                + calendar.get(Calendar.MINUTE) + "分";
    }

    public static String getCurrentDateSimpleFormat() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR) + "-"
                + (calendar.get(Calendar.MONTH) + 1) + "-"//从0计算
                + calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static int getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int getYear(String dateString) {
        String[] time = dateString.split("-");
        if (time.length == 3) {
            return Integer.parseInt(time[0]);
        }
        return -1;
    }

    public static int getMonth(String dateString) {
        String[] time = dateString.split("-");
        if (time.length == 3) {
            return Integer.parseInt(time[1]);
        }
        return -1;
    }

    public static int getDay(String dateString) {
        String[] time = dateString.split("-");
        if (time.length == 3) {
            return Integer.parseInt(time[2]);
        }
        return -1;
    }
}

